package br.com.cfccultura.admin.control.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;

import br.com.cfccultura.admin.model.dao.IAlunoDAO;
import br.com.cfccultura.admin.model.vo.Aluno;
import br.com.cfccultura.admin.model.vo.Anexo;
import br.com.cfccultura.admin.model.vo.Funcionario;

@Component("AnexoAlunoServlet")
public class AnexoAlunoServlet implements HttpRequestHandler {       

	@Autowired
	private IAlunoDAO alunoDAO;
	
	@Override
	public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String action = request.getParameter("action");
		if(action.equalsIgnoreCase("upload")) {
			upload(request);
			viewAllAttaches(request, response);
		} else if(action.equalsIgnoreCase("render")) {
			viewAllAttaches(request, response);
		}
	}
	
	private void upload(HttpServletRequest request) {
		List<FileItem> files = uploadFile(request);	
		
		Aluno vo = new Aluno(new Integer(request.getParameter("aluno")));
		vo.setFuncionarioCadastro(new Funcionario(new Integer(request.getParameter("usuario"))));
		
		for(FileItem file : files) {
			getAlunoDAO().incluirAnexo(vo, file);			
		}
	}
	
	private void viewAllAttaches(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer codigo = new Integer(request.getParameter("aluno"));
		StringBuilder html = new StringBuilder();
		List<Anexo> anexos = getAlunoDAO().consultaListaAnexos(codigo);
		if(anexos.size() > 0) {
			html.append("<div id=\"gallery\">");
			html.append("<table>");
			int qtde = 0;
			for(Anexo anexo : anexos) {
				if(qtde == 0) {
					html.append("<tr>");
				}
				html.append("	<td>");
				html.append("		<table>");
				html.append("			<tr>");
				html.append("				<td style='text-align: center'>");
				html.append("					<a href=\"anexo.do?method=view&tipo=2&icone=false&codigo=").append(anexo.getCodigo()).append("\" title=\"").append(anexo.getNome()).append("\">");
				html.append("						<img src=\"anexo.do?method=view&tipo=2&icone=true&codigo=").append(anexo.getCodigo()).append("\" alt=\"\" />");
				html.append("					</a>");
				html.append("				</td>");
				html.append("			</tr>");
				html.append("			<tr>");
				html.append("				<td style='text-align: center; font-size:11px'>").append(anexo.getNome()).append("</td>");
				html.append("			</tr>");
				html.append("			<tr>");
				html.append("				<td style='text-align: center'>"); 
				html.append("					<img src=\"_images/remove.png\" style='cursor:pointer' onclick='excluirAnexo(").append(anexo.getCodigo()).append(")'/>");
				html.append("				</td>");
				html.append("			</tr>");
				html.append("		</table>");
				html.append("	</td>");
				if(qtde == 7) {
					html.append("</tr>");
					qtde = 0;
				}
				qtde++;
			}
			html.append("</table>");
			html.append("</div>");
		} else {
			html.append("<p>Sem anexos</p>");
		}
		
		PrintWriter out = response.getWriter();
		out.write(html.toString());
	}
	
	private List<FileItem> uploadFile(HttpServletRequest request) {
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload fileUpload = new ServletFileUpload(factory);
		List<FileItem> files = new ArrayList<FileItem>();
		try {
			@SuppressWarnings("unchecked")
			List<FileItem> fileItens = fileUpload.parseRequest(request);
			for(FileItem fileItem : fileItens) {
				if(fileItem.getName() != null && fileItem.getSize() > 0)
					files.add(fileItem);
			}
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
		return files;
	}
	
	public IAlunoDAO getAlunoDAO() {
		return alunoDAO;
	}
	public void setAlunoDAO(IAlunoDAO alunoDAO) {
		this.alunoDAO = alunoDAO;
	}
}
